--[[
Name: oUF_cRefactored
Author: Sonora (The Dragonflight, US Kirin Tor)
Website & SVN: http://code.google.com/p/browniehelper/source/browse/#svn/trunk/oUF_cRefactored
License: GNU General Public License v3
]]

--[[
Simple minded OnUpdate task scheduler.

Factored out here to make it easy to plug in a smarter implementation if there
is a need, we go Ace3 (was the old memory leak finally fixed?), etc.
]]

cRefactored.reactor = {
    calls = {},
    updateFrame = CreateFrame("Frame", "cRefactored.reactorFrame"),
    
    start = function(self)
        self.updateFrame:SetScript('OnUpdate', function() self:update() end)
        self.updateFrame:Show() 
    end,
        
    stop = function(self) self.updateFrame:Hide() end,
    
    registerCall = function(self, call, interval)
        self.calls[call] = (interval and {interval=interval, lastFire=GetTime()} or true) 
    end,
    
    unregisterCall = function(self, call) 
        if call then self.calls[call] = nil end
    end,
    
    update = function(self)
        for call, timer in pairs(self.calls) do
            if timer == true then call()
            else
                local currTime = GetTime()
                if (timer.lastFire + timer.interval) < GetTime() then 
                    call()
                    timer.lastFire = GetTime()
                end
            end
        end
    end,
}
